package com.company.链表;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author zhangjian
 * @data 2023/3/12
 * @Version 1.0
 * @Descripion
 */
public class AB11合并两个排序的链表 {
    public static void main(String[] args) {

    }
}

class Solution {
    public ListNode Merge(ListNode list1, ListNode list2) {

        List<Integer> list = new ArrayList<>();
        if (list1 == null) {
            return list2;
        }
        if (list2 == null) {
            return list1;
        }
        if (list1 == null && list2 == null) {
            return null;
        }
        // 将链表的值加入到集合中
        while (list1 != null) {
            list.add(list1.val);
            list1 = list1.next;
        }
        while (list2 != null) {
            list.add(list2.val);
            list2 = list2.next;
        }
        Collections.sort(list);
        // 将集合转成链表
        ListNode node = new ListNode(list.get(0));
        ListNode newNode = node;
        for (Integer integer : list) {
            newNode.next = new ListNode(integer);
            newNode = newNode.next;
        }
        return node;
    }
}


